<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\yperp\model;

use think\Model as ThinkModel;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client as GuzzleHttpClient;
use think\Db as ThinkDb;

/**
 * 模型
 * @package app\yperp\model
 */
class Goods extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'yperp_supplier_goods';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;


    function goodslist($supplierId)
    {
        $client = new GuzzleHttpClient([
            'verify' => env('root_path') . 'application\yperp\common\cacert.pem',
        ]);
        try {
            $response = $client->request('POST', 'https://gyl.scm121.com/api/goods/buyer/supplier/goods/list', [
                'json' => [
                    'sort' => 'spu.SALES_VOLUME DESC,spu.ID ASC',
                    'supplierId' => $supplierId,
                    'asyncFillField' => true,
                    'hasStock' => false,
                    'itemCodeVague' => '0',
                    'pageNum' => 1,
                    'pageSize' => 10000,
                    'styleCodeVague' => "0"
                ],
                'headers' => [
                    'Content-Type' => 'application/json',
                    'Accept' => 'application/json',
                    'authorization' => module_config('yperp.jst_token'),
                ]
            ]);
            $data = $response->getBody()->getContents();
            dump($supplierId);
            dump($data);
            $data = json_decode($data, true);

            if ($data['success'] == true) {
                foreach ($data['data'] as $value) {
                    $items['supplierId'] = $supplierId;
                    $items['spuId'] = $value['spuId'];
                    $items['itemCoId'] = $value['itemCoId'];
                    $items['itemName'] = $value['itemName'];
                    $items['itemSpuId'] = $value['itemSpuId'];
                    $items['saleSevenDesensitized'] = $value['saleSevenDesensitized'];
                    $items['saleThirtyDesensitized'] = $value['saleThirtyDesensitized'];
                    $items['styleCode'] = $value['styleCode'];
                    $items['supplierName'] = $value['supplierName'];
                    $items['shopName'] = $value['shopName'];
                    $items['brandId'] = $value['brandId'];
                    $items['brandName'] = $value['brandName'];
                    $items['categoryName'] = $value['categoryName'];
                    $items['created'] = $value['created'];
                    $items['updated'] = $value['updated'];

                    $where = ['supplierId' => $items['supplierId'], 'itemSpuId' => $items['itemSpuId'],  'itemCoId' => $items['itemCoId']];
                    $bool = self::where($where)->value('itemSpuId');
                    dump(['itemSpuId' => $items['itemSpuId'], 'itemCoId' => $items['itemCoId']]);
                    if ($bool) {
                        $res = self::where($where)->update($items);
                    } else {
                        $res = self::where($where)->create($items);
                    }

                    // Queue::push(GoodsDetailAsync::class, ['itemSpuId' => $items['itemSpuId'], 'itemCoId' => $items['itemCoId']]);
                    $this->goodsdetail($items['itemCoId'], $items['itemSpuId']);
                }
            }
        } catch (GuzzleException $e) {
            dump($e->getMessage());
            dump($e->getTraceAsString());
        }
    }
    function goodsdetail($itemCoId, $itemSpuId)
    {
        $client = new GuzzleHttpClient([
            'verify' => env('root_path') . 'application\yperp\common\cacert.pem',
        ]);
        dump([
            'itemCoId' => $itemCoId,
            'itemSpuId' => $itemSpuId,
        ]);
        try {
            $response = $client->request('POST', 'https://gyl.scm121.com/api/goods/goods/queryGoodsDetail', [
                'json' => [
                    'itemCoId' => $itemCoId,
                    'itemSpuId' => $itemSpuId,
                ],
                'headers' => [
                    'Content-Type' => 'application/json',
                    'Accept' => 'application/json',
                    'authorization' => module_config('yperp.jst_token'),
                ]
            ]);
            $data = $response->getBody()->getContents();

            $data = json_decode($data, true);
            dump($data);
            if ($data['success'] == true) {
                $value = $data['data'];

                $items['itemCoId'] = $itemCoId;
                $items['itemSpuId'] = $itemSpuId;
                $items['categoryId'] = $value['categoryId'];
                $items['categoryName'] = $value['categoryName'];
                $items['coId'] = $value['coId'];
                $items['created'] = $value['created'];
                $items['dealPriceNinetyMax'] = $value['dealPriceNinetyMax'];
                $items['itemCoId'] = $value['itemCoId'];
                $items['itemName'] = $value['itemName'];
                $items['itemDetailImages'] = json_encode($value['itemDetailImages']);
                $items['itemMainImage'] = json_encode($value['itemMainImage']);
                $items['itemSpuId'] = $value['itemSpuId'];
                $items['maxCostPrice'] = $value['maxCostPrice'];
                $items['maxSupplyPrice'] = $value['maxSupplyPrice'];
                $items['minCostPrice'] = $value['minCostPrice'];
                $items['minSalesPrice'] = $value['minSalesPrice'];
                $items['minSupplyPrice'] = $value['minSupplyPrice'];
                $items['saleFifteen'] = $value['saleFifteen'];
                $items['saleFifteenDesensitized'] = $value['saleFifteenDesensitized'];
                $items['saleSeven'] = $value['saleSeven'];
                $items['saleSevenDesensitized'] = $value['saleSevenDesensitized'];
                $items['saleThirty'] = $value['saleThirty'];
                $items['saleThree'] = $value['saleThree'];
                $items['saleThreeDesensitized'] = $value['saleThreeDesensitized'];
                $items['saleYesterday'] = $value['saleYesterday'];
                $items['saleYesterdayDesensitized'] = $value['saleYesterdayDesensitized'];
                $items['salesVolume'] = $value['salesVolume'];
                $items['salesVolumeDesensitized'] = $value['salesVolumeDesensitized'];
                $items['spuId'] = $value['spuId'];
                $items['supplierId'] = $value['supplierId'];
                $items['styleCode'] = $value['styleCode'];

                $this->goodsdetailsku($value['itemSkuList'], $itemCoId, $itemSpuId);

                $this->AreaFreight($itemCoId, $items);


                $bool = self::where(['itemSpuId' => $items['itemSpuId']])->value('itemSpuId');
                if ($bool) {
                    self::where(['itemSpuId' => $items['itemSpuId']])->update($items);
                } else {
                    self::where(['itemSpuId' => $items['itemSpuId']])->create($items);
                }
            }
        } catch (\Exception $e) {
            dump($e->getMessage());
            dump($e->getTraceAsString());
        }
    }

    function goodsdetailsku($datas, $itemCoId, $itemSpuId)
    {
        foreach ($datas as $value) {

            $items['id'] = $value['id'];
            $items['itemCoId'] = $itemCoId;
            $items['itemSpuId'] = $itemSpuId;
            $items['coId'] = $value['coId'];
            $items['spuId'] = $value['spuId'];

            $items['colour'] = $value['colour'];
            $items['costPrice'] = $value['costPrice'];
            $items['costPriceNew'] = $value['costPriceNew'];
            $items['dealPriceNinetyMax'] = $value['dealPriceNinetyMax'];
            $items['erpStyleCode'] = $value['erpStyleCode'];
            $items['itemCode'] = $value['itemCode'];
            $items['linePrice'] = $value['linePrice'];
            $items['pic'] = $value['pic'];
            $items['shortName'] = $value['shortName'];

            $items['stock'] = $value['stock'];
            $items['stockTime'] = $value['stockTime'];
            $items['supplyPrice'] = $value['supplyPrice'];


            $bool = ThinkDb::name('yperp_supplier_goods_detail_sku')->where(['id' => $items['id'], 'itemSpuId' => $items['itemSpuId']])->value('itemSpuId');
            if ($bool) {
                ThinkDb::name('yperp_supplier_goods_detail_sku')->where(['itemSpuId' => $items['itemSpuId']])->update($items);
            } else {
                ThinkDb::name('yperp_supplier_goods_detail_sku')->where(['itemSpuId' => $items['itemSpuId']])->insert($items);
            }
        }
    }
    function AreaFreight($supplierCoId, &$items)
    {
        $client = new GuzzleHttpClient([
            'verify' => env('root_path') . 'application\yperp\common\cacert.pem',
            // 忽略证书验证
        ]);
        try {
            $response = $client->request('GET', 'https://gyl.scm121.com/api/drp/supplier/logistics/queryAreaStartFreightV2', [
                'query' => [
                    'channelCoId' => '13659884',
                    'supplierCoId' => $supplierCoId,
                ],
                'headers' => [
                    'Content-Type' => 'application/json',
                    'Accept' => 'application/json',
                    'authorization' => module_config('yperp.jst_token'),
                ]
            ]);
            $data = $response->getBody()->getContents();
            dump($data);
            $data = json_decode($data, true);
            $items['banArea'] = json_encode($data['data']['banArea'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
            $items['freeArea'] = json_encode($data['data']['freeArea'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
            $items['normalArea'] = json_encode($data['data']['normalArea'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
            $items['remoteArea'] = json_encode($data['data']['remoteArea'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        } catch (GuzzleException $e) {
            dump($e->getMessage());
            dump($e->getTraceAsString());
        }
    }
}
